

/*
* Copyright @ 2017 com.iflysse.trains
* crm 下午1:43:46
* All right reserved.
*
*/
	
package com.qf.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qf.dto.CrmDepartmentDto;
import com.qf.dto.CrmStaffDto;
import com.qf.pojo.CrmDepartment;
import com.qf.pojo.CrmPost;
import com.qf.pojo.CrmStaff;
import com.qf.service.CrmDepartmentService;
import com.qf.service.CrmStaffService;
import com.qf.utils.DateUtil;

/**
* @desc: crm
* @descripe:XXXX
* @author: 周锐
* @createTime: 2017年7月25日 下午1:43:46
* @history:
* @version: v1.0

*/
@Controller
public class CrmStaffServlet {
   
	@Autowired
	@Qualifier("crmStaffService")
	private CrmStaffService staffService;
	@Autowired
	@Qualifier("crmDepartmentService")
	private CrmDepartmentService cs;

	
	/**
	* @param staffService the staffService to set
	*/
	
	public void setStaffService(CrmStaffService staffService) {
	
		this.staffService = staffService;
	}
	
	
	/**
	* @param cs the cs to set
	*/
	
	public void setCs(CrmDepartmentService cs) {
	
		this.cs = cs;
	}

	@RequestMapping("/addStaff.do")
	public ModelAndView addStaff(HttpServletRequest request,HttpServletResponse response) throws ParseException{
		ModelAndView mv=new ModelAndView();
		String loginName=request.getParameter("loginName");
		String loginPwd = request.getParameter("loginPwd");
		String staffName=request.getParameter("staffName");
		String gender = request.getParameter("gender");
		String postId = request.getParameter("postId");
		String onDutyDate_ = request.getParameter("onDutyDate");
		CrmStaff staff=new CrmStaff();
		staff.setLoginName(loginName);
		staff.setLoginPwd(loginPwd);
		staff.setStaffName(staffName);
		staff.setGender(gender);
		CrmPost post=new CrmPost();
		post.setPostId(Long.valueOf(postId));;
		staff.setCrmPost(post);
		staff.setOnDutyDate(DateUtil.tranfaceLong(onDutyDate_));
		staffService.addStaff(staff);
		mv.setViewName("findAllStaff.do");
		return mv;
		
	}
	@RequestMapping("/findAllStaff.do")
	public ModelAndView findAllStaff(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv=new ModelAndView();
		List<CrmStaffDto> allstaffs = staffService.findAllcrmstaffs();
		mv.addObject("allstaffs", allstaffs);
		mv.setViewName("/pages/staff/listStaff.jsp");
		return mv;
		
}
	@RequestMapping("/preUpdateStaff.do")
	public ModelAndView preUpdateStaff(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv=new ModelAndView();
		String staffId = request.getParameter("staffId");
		String loginName=request.getParameter("loginName");
		String loginPwd = request.getParameter("loginPwd");
		String staffName = request.getParameter("staffName");
		String gender= request.getParameter("gender");
		String onDutyDate = request.getParameter("onDutyDate");
		String depName= request.getParameter("depName");
		String postName = request.getParameter("postName");
		CrmStaffDto crmstaff=new CrmStaffDto(Long.valueOf(staffId),loginName,loginPwd,staffName,gender,onDutyDate,depName,postName);
		List<CrmDepartmentDto> deptList = cs.findAllDepartment();
		mv.addObject("crmstaff", crmstaff);
		mv.addObject("deptList", deptList);
		mv.setViewName("/pages/staff/editStaff.jsp");
		return mv;
}
	@RequestMapping("/updateStaff.do")
	public ModelAndView updateStaff(HttpServletRequest request,HttpServletResponse response) throws ParseException {
		ModelAndView mv=new ModelAndView();
		String staffId = request.getParameter("staffId");
		String loginName=request.getParameter("loginName");
		String loginPwd = request.getParameter("loginPwd");
		String staffName = request.getParameter("staffName");
		String gender= request.getParameter("gender");
		String onDutyDate = request.getParameter("onDutyDate");
		String depId= request.getParameter("depId");
		String postId = request.getParameter("postId");
		CrmStaff crmStaff = staffService.findbyId(Long.valueOf(staffId));
		crmStaff.setLoginName(loginName);
		crmStaff.setLoginPwd(loginPwd);
	    crmStaff.setStaffName(staffName);
	    crmStaff.setGender(gender);
	    crmStaff.setOnDutyDate(DateUtil.tranfaceLong(onDutyDate));
	    CrmDepartment crmDepartment = new CrmDepartment();
	    crmDepartment.setDepId(Long.valueOf(depId));
	    CrmPost crmPost = new CrmPost();
	    crmPost.setPostId(Long.valueOf(postId));
	    crmPost.setCrmDepartment(crmDepartment);
	    crmStaff.setCrmPost(crmPost);
	    staffService.updateStaff(crmStaff);
	    mv.setViewName("findAllStaff.do");
		return mv;	
}
	@RequestMapping("/findStaff.do")
	public ModelAndView findStaff(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		String staffName = request.getParameter("staffName");
		List<CrmStaffDto> allstaffs = staffService.findStaff(staffName);
		mv.addObject("allstaffs", allstaffs);
		mv.setViewName("/pages/staff/listStaff.jsp");
		return mv;
}
	@RequestMapping("/findByName.do")
	public @ResponseBody String findbyName(HttpServletRequest request,HttpServletResponse response) {
		String loginName=request.getParameter("loginName");
		CrmStaff crmStaff = staffService.findByName(loginName);
		if(crmStaff!=null){
			return "{\"flag\":\"1\"}";
		}else{
		return "{\"flag\":\"0\"}";
	}
	}

	@RequestMapping("/login.do")
	public ModelAndView login(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		String loginName=request.getParameter("loginName");
		String loginPwd=request.getParameter("loginPwd");
		CrmStaff crmStaff = staffService.findByName(loginName);
		if(crmStaff.getLoginPwd().equals(loginPwd)){
			request.getSession().setAttribute("crmStaff", crmStaff);
			mv.setViewName("/pages/frame.jsp");
		}else{
			request.setAttribute("msg", "密码错误");
			mv.setViewName("/pages/login.jsp");
		}
		return mv;
		
}
	@RequestMapping("/loginOut.do")
	public ModelAndView loginOut(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		request.getSession().invalidate();
		mv.setViewName("/pages/frame.jsp");
		return mv;
}
}